<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 广告接口控制器
 */
class Advertisement extends ApiBase
{
    
    /**
     * 获取广告列表
     */
    public function getAdvertisementList()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['position', 'page', 'limit', 'status']);
        return $this->apiReturn($this->logicAdvertisement->getAdvertisementList($filteredData));
    }
    
    /**
     * 获取首页广告列表
     */
    public function getHomeAdvertisementList()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['position', 'limit']);
        return $this->apiReturn($this->logicAdvertisement->getHomeAdvertisementList($filteredData));
    }
    
    /**
     * 记录广告点击
     */
    public function recordAdvertisementClick()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['id', 'user_token']);
        return $this->apiReturn($this->logicAdvertisement->recordAdvertisementClick($filteredData));
    }
    
    /**
     * 过滤API参数，只保留指定的业务字段
     */
    private function filterApiParams($data, $allowedFields)
    {
        $filtered = [];
        foreach ($allowedFields as $field) {
            if (isset($data[$field])) {
                $filtered[$field] = $data[$field];
            }
        }
        return $filtered;
    }
}